<template>
  <div>
    <el-button-group>
      <el-button
        size="small"
        type="primary"
        @click="handleClick(1)"
        :disabled="addDisabled"
        >新增子部门</el-button
      >
      <el-button
        size="small"
        type="primary"
        @click="handleClick(2)"
        :disabled="disabled"
        >编辑</el-button
      >
      <el-button
        size="small"
        type="danger"
        @click="handleClick(3)"
        :disabled="disabled"
        >删除</el-button
      >
    </el-button-group>

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="550px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="排序">
            <el-input-number
              v-model="form.sort"
              :min="1"
              :max="1000"
              size="medium"
              label="排序"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="描述">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.content"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button  type="custom" v-preventReClick @click="submitForm('form')">提 交</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  tbDeptAdd,
  tbDeptDelete,
  tbDeptUpdate,
} from "@/api/admin/user/department-manage.js";
import Bus from "@/utils/bus.js";

export default {
  data() {
    return {
      title: "新增",
      checkedData: [],
      dialogVisible: false,
      addOrEdit: 1,
      disabled: false,
      addDisabled: false,
      form: {
        type: 2, // 1 学生管理 2 部门管理
        pid: "",
        sort: 10,
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入部门名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    checkedData(val) {
      if(this.checkedData.id == 0){
        this.disabled = true;
      }else{
        this.disabled = false;
        let deptFullPath = this.checkedData.deptFullPath;
        this.addDisabled = deptFullPath.split("/").length >= 5;
      }
    },
  },
  mounted() {
    Bus.$on("selectedData", (v) => {
      this.checkedData = v;
    });
  },
  methods: {
    //显示弹窗
    handleAddOrEdit() {
      if (this.addOrEdit == 2) {
        this.title = "编辑";
        this.form.id = this.checkedData.id;
        this.form.pid = this.checkedData.pid;
        this.form.sort = this.checkedData.sort;
        this.form.name = this.checkedData.name;
        this.form.content = this.checkedData.content;
      } else {
        this.title = "新增";
        this.form.pid = this.checkedData.id;
        delete this.form.id;
      }
      this.dialogVisible = true;
    },
    //提交
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.deptAdd() : this.deptEdit();
        }
      });
    },
    //新增
    deptAdd() {
      tbDeptAdd(this.form).then((res) => {
        if (res.data.code == 0) {
          this.dialogVisible = false;
          Bus.$emit("refreshList");
          this.$message({
            message: "操作成功",
            type: "success",
          });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    //编辑
    deptEdit() {
      tbDeptUpdate(this.form).then((res) => {
        if (res.data.code == 0) {
          this.dialogVisible = false;
          Bus.$emit("refreshList");
          this.$message({
            message: "操作成功",
            type: "success",
          });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    //删除
    deptDelete() {
      let { id, name } = this.checkedData;
      let queryList = {
        id,
        name,
        type: this.form.type,
      };
      tbDeptDelete(queryList).then((res) => {
        if (res.data.code == 0) {
          Bus.$emit("refreshList");
          this.$message({
            message: "删除成功",
            type: "success",
          });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    // 删除确认
    handleDelete() {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deptDelete();
        })
        .catch(() => {});
    },
    //验证
    handleClick(type) {
      this.addOrEdit = type;
      if (!this.checkedData.length) {
        this.$message.error("请选择部门");
        return;
      }
      switch (type) {
        case 1:
        case 2:
          this.handleAddOrEdit();
          break;

        case 3:
          this.handleDelete();
          break;
        default:
          return;
      }
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields();
      this.form.sort = 10;
      this.form.content = "";
      this.form.name = "";
    },
  },
};
</script>


